package com.studentmanagement.service;

import com.studentmanagement.dao.StudentMapper;
import com.studentmanagement.model.Student;
import com.studentmanagement.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentService {
    public Student getStudentById(Integer id) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            return mapper.getStudentById(id);
        }
    }
    
    public Student getStudentByUserId(Integer userId) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            return mapper.getStudentByUserId(userId);
        }
    }
    
    public Student getStudentByStudentId(String studentId) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            return mapper.getStudentByStudentId(studentId);
        }
    }
    
    public List<Student> getAllStudents() {
        try (SqlSession session = MyBatisUtil.openSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            return mapper.getAllStudents();
        }
    }
    
    public List<Student> searchStudents(String name, String department, String major) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            Map<String, Object> params = new HashMap<>();
            if (name != null && !name.trim().isEmpty()) {
                params.put("name", name);
            }
            if (department != null && !department.trim().isEmpty()) {
                params.put("department", department);
            }
            if (major != null && !major.trim().isEmpty()) {
                params.put("major", major);
            }
            return mapper.searchStudents(params);
        }
    }
    
    public boolean insertStudent(Student student) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            int result = mapper.insertStudent(student);
            session.commit();
            return result > 0;
        }
    }
    
    public boolean updateStudent(Student student) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            int result = mapper.updateStudent(student);
            session.commit();
            return result > 0;
        }
    }
    
    public boolean deleteStudent(Integer id) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            int result = mapper.deleteStudent(id);
            session.commit();
            return result > 0;
        }
    }
    
    public boolean deleteStudentByUserId(Integer userId) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            int result = mapper.deleteStudentByUserId(userId);
            session.commit();
            return result > 0;
        }
    }
} 